package light.mvc.controller.doctor;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.base.Json;
import light.mvc.page.base.Status;
import light.mvc.page.doctor.ChargeChemicalExam;
import light.mvc.page.doctor.RegisterCharge;
import light.mvc.service.doctor.ChargeChemicalExamServiceI;
import light.mvc.service.patient.RegisterSeqServiceI;
import light.mvc.utils.TimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/chargeChemicalExam")
public class ChargeChemicalExamController extends BaseController {
	@Autowired
	private ChargeChemicalExamServiceI chargeChemicalExamService;
	
	@Autowired
	private RegisterSeqServiceI registerSeqService;
	
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("errorCode", "1");
			return "/doctor/chargeChemicalExam";
		}
		request.setAttribute("msg", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeSegment));
		request.setAttribute("errorCode", "0");
		return "/doctor/chargeChemicalExam";
	}
	
	@RequestMapping("/detailPage")
	public String detailPage(HttpServletRequest request,Integer registerId) {
		List<RegisterCharge> registerCharges = chargeChemicalExamService.getRegisterCharge(registerId);
		request.setAttribute("registerCharges", registerCharges);
		request.setAttribute("registerId", registerId);
		return "/doctor/chargeChemicalExamDetail";
	}
	
	@RequestMapping("/historyPage")
	public String historyPage(HttpServletRequest request,Integer patientId,Date timeSegment) {
		List<RegisterCharge> registerCharges = chargeChemicalExamService.getRegisterChargeByPatientId(patientId,timeSegment);
		request.setAttribute("registerCharges", registerCharges);
		return "/doctor/chargeChemicalExamHistory";
	}
	
	@RequestMapping("/addCharge")
	@ResponseBody
	public Json addCharge(Integer chemicalExamId,Integer registerId) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			chargeChemicalExamService.addCharge(registerId, chemicalExamId);
			errorCode = 0;
			errorMsg = "开单成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/charge")
	@ResponseBody
	public Json charge(Integer registerChargeId) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			chargeChemicalExamService.charge(registerChargeId);
			errorCode = 0;
			errorMsg = "缴费成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/addChemicalExamPage")
	public String addChemicalExamPage(HttpServletRequest request,Integer registerChargeId,String chemicalExamResult) {
		request.setAttribute("registerChargeId", registerChargeId);
		request.setAttribute("chemicalExamResult", chemicalExamResult);
		return "/doctor/chargeChemicalExamAdd";
	}
	
	@RequestMapping("/chemicalExam")
	@ResponseBody
	public Json chemicalExam(ChargeChemicalExam chargeChemicalExam) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		String result = chargeChemicalExam.getResult();
		result = result.replaceAll("\\r\\n", "<br>");
		result = result.replaceAll("\\s", "&nbsp;");
		chargeChemicalExam.setResult(result);
		try {
			chargeChemicalExamService.addChemicalExam(chargeChemicalExam);
			errorMsg = "化验成功";
			errorCode = 0;
			j = new Json(new Status(errorCode, errorMsg),null);
		} catch (Exception e) {
			errorMsg = "化验失败："  + e.getMessage();
			j = new Json(new Status(errorCode, errorMsg),null);
		}
		
		return j;
		
	}

}
